<?php
class CV{
	private static $zend_framework_dir;
	private static $dependencies_dir;
	private static $inited = false;	
	public static function autoload( $class ){
		$autoload_map = array(
			'ListComprehension' => self::$dependencies_dir.'php-lc/ListComprehension.php',
		);
		if( isset($autoload_map[$class]) ){
			require $autoload_map[$class];
			return true;
		}
		
		$namespace = explode( "_", $class );
		$class_file = implode(DIRECTORY_SEPARATOR, $namespace) . ".php";
		$this_dir = dirname(__FILE__) . DIRECTORY_SEPARATOR;
		
		switch( $namespace[0] ){
			case 'Zend':
				$file = self::$zend_framework_dir.$class_file;
				if( file_exists($file) ){
					require $file;
					return true;
				}
				
				// assume ZendFramework/library/ it is in include_path
				@include $class_file;
				return class_exists( $class, false );
			case 'CV':
				$file = $this_dir.$class_file;
				if( file_exists($file) ){
					require $file;
					return true;
				}
		}

		if( defined('CV_SIMPLETEST_DIR') && CV::simpletest_autoload($class) ){
			return true;
		}		
		return false;
	}
	public static function simpletest_autoload( $class ){
		if( !defined('CV_SIMPLETEST_DIR')  ){
			$path = CV::realpath( self::$dependencies_dir, 'simpletest' ); // default directory
			if( $path ){
				define( 'CV_SIMPLETEST_DIR', $path.DIRECTORY_SEPARATOR );
			} else {
				trigger_error( 'Please checkout cvphplib including dependencies or define CV_SIMPLETEST_DIR as the directory containing simpletest.php etc.', E_USER_ERROR );
			}
		}
		static $autoload_map = array(
			'TestSuite' => 'test_case'
			,'SimplePatternCollector' => 'collector'
			,'UnitTestCase' => 'unit_tester'
			,'WebTestCase' => 'web_tester'
			,'SimpleBrowser' => 'browser'
			,'DomTestCase' => 'extensions/dom_tester'
			,'TextReporter' => 'reporter'
			,'Mock' => 'mock_objects'
		);
		if( isset($autoload_map[$class]) ){
			require CV_SIMPLETEST_DIR.$autoload_map[$class].'.php';
			return true;
		}
		return false;
	}
	public static function add_include_path( $path ){
		set_include_path( get_include_path().PATH_SEPARATOR.$path );
	}
	public static function init( $options = array() ){
		
		// Load libraries
		require dirname( __FILE__ ).'/../environment.inc.php';
		
		
		if( self::$inited ){
			return;
		}
		self::$inited = true;
		if( isset($options['dependencies_dir']) ){
			self::$dependencies_dir = $options['dependencies_dir'];
		}else {
			self::$dependencies_dir = CV::realpath( dirname(__FILE__), '..', '..', 'dependencies' ).DIRECTORY_SEPARATOR;
		}
				
		if( defined('ZENDFRAMEWORK_DIR')  ){
			self::$zend_framework_dir = ZENDFRAMEWORK_DIR;
		} else {
			// use dependencies directory if available					
			$this_dir = dirname(__FILE__) . DIRECTORY_SEPARATOR;
			self::$zend_framework_dir = CV::realpath( self::$dependencies_dir, 'ZendFramework', 'library').DIRECTORY_SEPARATOR;
		}
		set_include_path( get_include_path().PATH_SEPARATOR.self::$zend_framework_dir );
		
		static $done = false; // <- in doubt? Read PHP Manual! 
		if( !$done ){
			spl_autoload_register( array('CV', 'autoload') );
			$done = true;
		}
		if( !defined('CV_SIMPLETEST_DIR') ){
			define( 'CV_SIMPLETEST_DIR', CV::realpath( self::$dependencies_dir, 'simpletest' ).DIRECTORY_SEPARATOR );
		}
		if( !defined('CV_DIR') ){
			define( 'CV_DIR', dirname(__FILE__).DIRECTORY_SEPARATOR );
		}
	}
	public static function realpath(){
		$args = func_get_args();
		return realpath( implode(DIRECTORY_SEPARATOR, $args) );
	}
	public static function path(){
		$args = func_get_args();
		$path = implode(DIRECTORY_SEPARATOR, $args);
		return preg_replace( '/[\\\\\/]+/', DIRECTORY_SEPARATOR, $path );
	}
	public static function array_keys( $array ){
		return array_keys( CV_Collection::unwrap($array) );
	}
}